package com.xzh.mallorderapi.util;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Collection;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

/**
 * 功能：工具类
 *
 * @author 千堆雪
 * @version 1.0.0
 * created on 2025/4/21 0:10, last modified on 2025/4/21 0:10
 */
public class XinUtils {
    private static final Pattern UNDERSCORE_TO_CAMEL_PATTERN = Pattern.compile("_(\\w)\\w*?");
    private static final Pattern CAMEL_TO_UNDERSCORE_PATTERN = Pattern.compile("([A-Z])");

    //判断字符串是否为空
    public static boolean isEmpty(String str) {
        return str == null || str.isEmpty();
    }

    //判断字符串是否非空
    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    //判断数组是否为空
    public static <T> boolean isEmpty(T[] arr) {
        return arr == null || arr.length == 0;
    }

    //判断数组是否非空
    public static <T> boolean isNotEmpty(T[] arr) {
        return !isEmpty(arr);
    }

    //判断集合是否为空
    public static boolean isEmpty(Collection<?> collection) {
        return collection == null || collection.isEmpty();
    }

    //判断集合是否非空
    public static boolean isNotEmpty(Collection<?> collection) {
        return !isEmpty(collection);
    }

    //判断map是否为空
    public static boolean isEmpty(Map<?, ?> map) {
        return map == null || map.isEmpty();
    }

    //判断map是否非空
    public static boolean isNotEmpty(Map<?, ?> map) {
        return !isEmpty(map);
    }

    //满足条件则抛出异常
    public static void throwIf(boolean condition, String message) {
        if (condition) {
            throw new RuntimeException(message);
        }
    }

    //判断对象是否为空
    public static boolean isEmpty(Object object) {
        if (object instanceof String str) {
            return isEmpty(str);
        }

        if (object instanceof Object[] arr) {
            return isEmpty(arr);
        }

        if (object instanceof Collection<?> collection) {
            return isEmpty(collection);
        }

        if (object instanceof Map<?, ?> map) {
            return isEmpty(map);
        }

        return object == null;
    }

    //判断对象是否非空
    public static boolean isNotEmpty(Object object) {
        return !isEmpty(object);
    }

    //判断对象是否全空
    public static boolean isAllEmpty(Object... objects) {
        return Stream.of(objects).allMatch(XinUtils::isEmpty);
    }

    //判断对象是否全非空
    public static boolean isAllNotEmpty(Object... objects) {
        return Stream.of(objects).allMatch(XinUtils::isNotEmpty);
    }

    //下划线转驼峰式
    public static String underscoreToCamel(String input) {
        Matcher matcher = UNDERSCORE_TO_CAMEL_PATTERN.matcher(input);
        StringBuilder sb = new StringBuilder();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    //驼峰式转下划线
    public static String camelToUnderscore(String input) {
        Matcher matcher = CAMEL_TO_UNDERSCORE_PATTERN.matcher(input);
        StringBuilder sb = new StringBuilder();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(1).toLowerCase());
        }
        matcher.appendTail(sb);
        String result = sb.toString();
        if (result.startsWith("_")) {
            result = result.substring(1);
        }
        return result;
    }


    /**
     * 将异常堆栈转化为字符串
     *
     * @param ex 异常对象
     * @return 异常字符串
     */
    public static String exceptiontoString(Throwable ex) {
        StringWriter stackTrace = new StringWriter();
        ex.printStackTrace(new PrintWriter(stackTrace));
        stackTrace.flush();
        return stackTrace.toString();
    }
}
